package com.huifer.wgx.controller;

import com.huifer.wgx.model.InteriorPointEntity;
import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.service.InterPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : InteriorPointController </p>
 * <p>Description : 内业数据处理点</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("interior_point")
@Api(tags = "InteriorPointController", description = "内业数据点管理")
public class InteriorPointController {

    @Autowired
    private InterPointService interPointService;
    private CommonResult commonResult = new CommonResult();


    @ApiOperation("外业点转内业点")
    @PostMapping("/out_point_to_inter")
    public ResponseEntity saveInterPoint(
            @RequestParam("work_id") Long workId,
            @RequestParam("personnel_id") Long personnelId
    ) {
        try {
            return ResponseEntity
                    .ok(commonResult
                            .success(interPointService.field2InterPoint(workId, personnelId)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("创建一个内业点")
    @PostMapping("/save")
    public ResponseEntity saveInterPoint(
             InteriorPointEntity iPoint
    ) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(interPointService.saveOne(iPoint)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("更新一个内业点")
    @PutMapping("/update")
    public ResponseEntity updateInterPoint(
       InteriorPointEntity iPoint
    ) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(interPointService.updateIpoint(iPoint)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("查询所有内业点")
    @GetMapping("/find_all")
    public ResponseEntity findAll(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(interPointService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("根据工程id查询内业数据点")
    @GetMapping("/find_by_work_id")
    public ResponseEntity findRtkByWorkId(
            @RequestParam("workid") Long workId,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult
                            .success(interPointService.findByWorkId(pageNum, pageSize, workId)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("根据内业人员id查询内业数据点")
    @GetMapping("/find_by_personnel_id")
    public ResponseEntity findByPersonnelId(
            @RequestParam("personnel_id") Long personnelId,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(
                            interPointService.findByPersonnelId(personnelId, pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


}
